﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;

namespace RogerioCoimbra.DivulgacaoEspirita.Site
{
	public partial class MultilineText_Field : System.Web.DynamicData.FieldTemplateUserControl
	{
		const int ComprimentoMaximo = 50;

		protected override void OnDataBinding(EventArgs e)
		{
			base.OnDataBinding(e);

			string campoString;

			if (	Request.Url.Segments.Any(segment => segment.Contains("Listar"))
				&&	!string.IsNullOrEmpty(FieldValueString)
				&&	FieldValueString.Length > ComprimentoMaximo
				&&	
					(
							(Table.Name == "Frase"	&& Column.Name == "Conteudo")
						||	(Table.Name == "Jornal"	&& Column.Name == "AssuntosAbordados")
					)
				)
				campoString = String.Format("{0}...", FieldValueString.Substring(0, ComprimentoMaximo));
			else
				campoString = FieldValueString;

			Literal1.Text = InserirParagrafos(campoString);
		}

		public override Control DataControl
		{
			get
			{
                return Literal1;
			}
		}

        /// <summary>
        /// Insere paragrafos no texto.
        /// </summary>
        /// <param name="texto"></param>
        /// <returns></returns>
        protected string InserirParagrafos(string texto)
        {
            return texto.Replace("\n", "<br />");
        }
	}
}
